package com.allnas.jpa.repository;

import com.allnas.jpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zhaojin
 */
@Transactional(readOnly = true)
public interface UserRepository extends JpaRepository<User,Integer> {
    /**
     * 根据用户名查询
     * @param userName
     * @return
     */
    @Query(nativeQuery = true, value = "select * from t_user where user_name = ?1")
    User findByUserName(String userName);

    /**
     * 根据年龄删除
     * @param age
     */
    @Modifying
    @Transactional(rollbackFor = Exception.class)
    @Query("delete from User u where u.age > ?1")
    void deleteByAge(Integer age);
}
